﻿using Microsoft.AspNetCore.Identity;
using NaturalNote.Core.Database;

namespace XxSystem.UsersServer.Ids4Svr
{
    public class XUserStore : IUserStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>
    {
        IDataQuery _dataQuery;
        IDataStorage _dataStorage;

        public XUserStore(IDataQuery dataQuery, IDataStorage dataStorage)
        {
            this._dataQuery = dataQuery;
            this._dataStorage = dataStorage;
        }



        public async Task<IdentityResult> CreateAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            await _dataStorage.Save(user);
            return IdentityResult.Success;
        }

        public async Task<IdentityResult> DeleteAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            await _dataStorage.Delete(user);
            return IdentityResult.Success;
        }

        public void Dispose()
        {

        }

        public async Task<ApplicationUser> FindByIdAsync(string userId, CancellationToken cancellationToken)
        {
            var rs = await _dataQuery.Get<ApplicationUser>("select * from nn_user where Id=@id", new { id = userId });
            return rs;
        }

        public async Task<ApplicationUser> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken)
        {
            var rs = await _dataQuery.Get<ApplicationUser>("select * from nn_user where NormalizedUserName=@normalizedUserName", new { normalizedUserName });
            return rs;
        }

        public async Task<string> GetNormalizedUserNameAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            return user.NormalizedUserName;
        }

        public async Task<string> GetPasswordHashAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            return user.PasswordHash;
        }

        public async Task<string> GetUserIdAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            return user.Id.ToString();
        }

        public async Task<string> GetUserNameAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            return user.UserName;
        }

        public async Task<bool> HasPasswordAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            return !string.IsNullOrWhiteSpace(user.PasswordHash);
        }

        public async Task SetNormalizedUserNameAsync(ApplicationUser user, string normalizedName, CancellationToken cancellationToken)
        {
            user.NormalizedUserName = normalizedName;
        }

        public async Task SetPasswordHashAsync(ApplicationUser user, string passwordHash, CancellationToken cancellationToken)
        {
            user.PasswordHash = passwordHash;
        }

        public async Task SetUserNameAsync(ApplicationUser user, string userName, CancellationToken cancellationToken)
        {
            user.UserName = userName;
        }

        public async Task<IdentityResult> UpdateAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            await _dataStorage.Update(user);
            return IdentityResult.Success;
        }

    }
}
